define(function (require) {

    require('./nav-tpl');
    require('./nav-top-tpl');
    var juicer = juicer || window.juicer;
    var navHtml = "";
    var isShop = location.href.indexOf("/shop/") > -1;
    var isOrder = location.href.indexOf("/order/") > -1;
    var sidebarTpl = juicer.templates['nav.juicer.html'];
    var navTpl = juicer.templates['nav-top.juicer.html'];
    juicer.register('getOuterUrl', CRM.getOuterUrl);

    var sidebar = {
        init: function () {
            var userInfo;
            userInfo = this.getUserInfoById();
            $(document).on('click', '.go-login', function () {
                CRM.goLogin();
            });
            nav.init(userInfo);
        },
        /**
         * 获取用户信息
         */
        getUserInfoById: function () {
            var self = this;
            var userInfo;
            CRM.request({
                url: CRM.getOuterUrl('/member/getUserbyUserId.do?userId=' + $.cookie('_uk')),
                type: 'get',
                async : false,
                success: function (data) {
                    if (data.status) {
                        navHtml = sidebarTpl({
                            path: (isShop || isOrder) ? '../..' : '..',
                            mobile: $.cookie('_cm'),
                            level: data.entry.level,
                            brand_shop_url: CRM.getOuterUrl("/buyer/shop/brand/imagemanage/index.html"),
                            tracker_url: CRM.getOuterUrl("/buyer/crm/php/o2o/seller/admin/search_remark.php"),
                            tracker_config_url: CRM.getOuterUrl("/buyer/crm/php/o2o/seller/admin/replyTypeGroup_config.html"),
                            crm_url: CRM.getOuterUrl("/buyer/crm/php/o2o/admin/"),
                            crm_url2: CRM.getOuterUrl("/buyer/crm/php/o2o/"),
                            crm_url3: "http://song.52shangou.com/o2o/crm/bindShop.php?userId=" + $.cookie('_uk'),
                            crm_url4: CRM.getOuterUrl("/logistics/monitor/index.html"),
                            base_url: CRM.getOuterUrl("/")
                        });
                        userInfo = data.entry;
                        self.build();
                    } else {
                        CRM.showDialog("获取登录信息失败，请重新登录！");
                        self.build();
                        //setInterval(CRM.goLogin(),5*1000);
                    }
                }
            });
            return userInfo;
        },
        build: function () {
            $("#main-sidebar").html(navHtml);
        }
    };


    var nav = {
        init: function (userInfo) {
            if(userInfo){
                CRM.request({
                    url: CRM.getOuterUrl('/org/getUserEmployeeListByOrgCode.do'),
                    type: 'get',
                    data:{
                        orgCode:"0010",
                        onlyInWork:true,
                        includeChild:true
                    },
                    success: function (data) {
                        if (data.status) {
                            var _isCS = false
                            $.each(data.entry,function(index,user){
                                if(user.userId == $.cookie('_uk')){
                                    _isCS = true;
                                }
                            });
                            //_isCS = false;//暂时不开放这个功能，先隐藏掉
                            $(".navbar-nav").html(navTpl({
                                isCS:_isCS,
                                userInfo:userInfo
                            }));
                        } else {
                            CRM.showDialog("获取组织信息失败，请重新登录！");
                        }
                    }
                });
            }
        }
    };
    sidebar.init();
});